import com.mysql.cj.jdbc.MysqlDataSource;
import view.RoomTable;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Scanner;

//登录进去的界面，菜单条里面有三个菜单，分别为房间信息，订餐信息和工作人员信息
class SystemDemo {
    public SystemDemo() {
        JFrame jFrame = new JFrame("酒店管理系统");
        //房间的菜单项
        JMenuItem ItemAdd1 = new JMenuItem("入住登记");
        JMenuItem ItemOut = new JMenuItem("退房登记");
        JMenuItem ItemQuery1 = new JMenuItem("查询入住情况");
        JMenuItem ItemCalculate1 = new JMenuItem("结算价格");
        //订餐的菜单项
        JMenuItem ItemOrder = new JMenuItem("订餐");
        JMenuItem ItemCancel = new JMenuItem("取消订餐");
        JMenuItem ItemQuery2 = new JMenuItem("查询订餐情况");
        JMenuItem ItemCalculate2 = new JMenuItem("结算餐饮价钱");
        //工作人员的菜单项
        JMenuItem ItemAdd3 = new JMenuItem("添加员工信息");
        JMenuItem ItemDelete = new JMenuItem("删除离职员工信息");
        JMenuItem ItemUpdate = new JMenuItem("修改员工信息");
        JMenuItem ItemQuery3 = new JMenuItem("查询员工信息");
        //设置三个菜单
        JMenu MenuRoom = new JMenu("房间信息");
        JMenu MenuFood = new JMenu("餐饮信息");
        JMenu MenuStaff = new JMenu("员工信息");
        //把房间的相关信息添加到房间的菜单项中
        MenuRoom.add(ItemAdd1);
        MenuRoom.add(ItemOut);
        MenuRoom.add(ItemQuery1);
        MenuRoom.add(ItemCalculate1);
        //把订餐的相关信息添加到餐饮的菜单项中
        MenuFood.add(ItemOrder);
        MenuFood.add(ItemCancel);
        MenuFood.add(ItemQuery2);
        MenuFood.add(ItemCalculate2);
        //把员工的相关信息添加到员工信息的菜单项中
        MenuStaff.add(ItemAdd3);
        MenuStaff.add(ItemDelete);
        MenuStaff.add(ItemUpdate);
        MenuStaff.add(ItemQuery3);
        //添加事件
        listener lis = new listener();
        ItemAdd1.addActionListener(lis);
        ItemOut.addActionListener(lis);
        ItemQuery1.addActionListener(lis);
        ItemCalculate1.addActionListener(lis);
        ItemOrder.addActionListener(lis);
        ItemCancel.addActionListener(lis);
        ItemQuery2.addActionListener(lis);
        ItemCalculate2.addActionListener(lis);
        ItemAdd3.addActionListener(lis);
        ItemDelete.addActionListener(lis);
        ItemUpdate.addActionListener(lis);
        ItemQuery3.addActionListener(lis);
        //设置菜单栏
        JMenuBar jMenuBar = new JMenuBar();
        //把三个菜单项添加到菜单栏
        jMenuBar.add(MenuRoom);
        jMenuBar.add(MenuFood);
        jMenuBar.add(MenuStaff);
        jFrame.add(jMenuBar);
        jFrame.setVisible(true);
        //jFrame.pack();
        jFrame.setBounds(600, 400, 600, 150);
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
    }

    public class listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            Scanner sc = new Scanner(System.in);
            Connection con = null;
            Statement sta = null;
            ResultSet rs = null;
            try {
                //连接数据库
                DataSource dataSource = new MysqlDataSource();
                ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/hotel?characterEncoding=utf8&useSSL=false");
                ((MysqlDataSource) dataSource).setUser("root");
                ((MysqlDataSource) dataSource).setPassword("abc123");

                con = dataSource.getConnection();
                sta = con.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("连接数据库失败");
            }
            String s = event.getActionCommand();
            try {
                switch (s) {
                    case "入住登记":
                        rs = sta.executeQuery("select Rno from Room");
//                        while (rs.next()) {
//                            System.out.print("  已入住房间:" + rs.getInt("Rno"));
//                        }
                        new RoomTable(rs);

//                        System.out.println();
                        String sql = "insert into Room values(?,?,?,?)";
                        PreparedStatement p1 = con.prepareStatement(sql);
                        System.out.println("输入房间号101~210，入住客户，房间型号（小，中，大），价格（100，150，200）：");
                        p1.setInt(1, sc.nextInt());
                        p1.setString(2, sc.next());
                        p1.setString(3, sc.next());
                        p1.setInt(4, sc.nextInt());
                        p1.executeUpdate();
                        System.out.println("入住登记成功");
                        break;
                    case "退房登记":
                        String sql2 = "delete from Room where Rno=?";
                        PreparedStatement p2 = con.prepareStatement(sql2);
                        System.out.println("输入退房的房间号：");
                        int DRno = sc.nextInt();
                        p2.setInt(1, DRno);
                        p2.executeUpdate();
                        System.out.println("退房登记成功");//退房时会同时在数据库中删除该房间所订餐饮的记录
                        break;
                    case "查询入住情况":
                        System.out.println("目前入住情况：");
                        rs = sta.executeQuery("select * from Room");
                        while (rs.next()) {
                            System.out.println("房间号:" + rs.getInt("Rno") + "\t入住客户:" + rs.getString("Pname") + "\t房间型号:" + rs.getString("Rsize") + "\t价格:" + rs.getInt("Price"));
                        }
                        break;
                    case "结算价格":
                        System.out.println("输入所结算房间价格的房间号：");
                        int n = sc.nextInt();
                        sta = con.createStatement();
                        rs = sta.executeQuery("select Price from Room where Rno='" + n + "'");
                        while (rs.next()) {
                            System.out.println("房间价格为：" + rs.getString("Price"));
                        }
                        break;
                    case "订餐":
                        String sql5 = "insert into Food values(?,?)";
                        PreparedStatement p5 = con.prepareStatement(sql5);
                        System.out.println("输入点餐客户房间号和食物号：");
                        p5.setInt(1, sc.nextInt());
                        p5.setInt(2, sc.nextInt());
                        p5.executeUpdate();
                        System.out.println("点餐成功");
                        break;
                    case "取消订餐":
                        String sql6 = "delete from Food where Rno=? and Fno=? ";
                        PreparedStatement p6 = con.prepareStatement(sql6);
                        System.out.println("输入取消订餐的房间号和食物号：");
                        p6.setInt(1, sc.nextInt());
                        p6.setInt(2, sc.nextInt());
                        p6.executeUpdate();
                        System.out.println("取消订餐成功");
                        break;
                    case "查询订餐情况":
                        System.out.println("目前的订餐情况为：");
                        rs = sta.executeQuery("select * from Food");
                        while (rs.next()) {
                            System.out.println("房间号:" + rs.getInt("Rno") + "\t所订食物           号:" + rs.getInt("Fno"));
                        }
                        break;
                    case "结算餐饮价钱":
                        System.out.println("输入需要结算餐饮的房间号：");
                        int FoodPrice = sc.nextInt();
                        sta = con.createStatement();
                        rs = sta.executeQuery("SELECT Rno,food.Fno,Price\n" + "from Food,`order`\n" + "where Rno='" + FoodPrice + "' and food.Fno=`order`.Fno");
                        System.out.println("所需结算餐饮的房间号、食物号和价格为：");
                        while (rs.next()) {
                            System.out.println("房间号：" + rs.getInt("Rno") + "\t食物号：" + rs.getInt("Fno") + "\t食物价格：" + rs.getString("Price"));
                        }
                        break;
                    case "添加员工信息":
                        String sql10 = "insert into Staff values(?,?,?,?,?)";
                        PreparedStatement p10 = con.prepareStatement(sql10);
                        System.out.println("输入要添加员工的工号，姓名，电话号码，年龄和地址：");
                        p10.setString(1, sc.next());
                        p10.setString(2, sc.next());
                        p10.setString(3, sc.next());
                        p10.setInt(4, sc.nextInt());
                        p10.setString(5, sc.next());
                        p10.executeUpdate();
                        System.out.println("添加成功");
                        System.out.println("现有员工信息：");
                        rs = sta.executeQuery("select * from Staff");
                        while (rs.next()) {
                            System.out.println("工号:" + rs.getString("Sno") + "\t姓名:" + rs.getString("Sname") + "\t电话号码:" + rs.getString("Sphone") + "\t年龄:" + rs.getInt("Sage") + "\t地址:" + rs.getString("Saddress"));
                        }
                        break;
                    case "删除离职员工信息":
                        String sql11 = "delete from Staff where Sno=? ";
                        PreparedStatement p11 = con.prepareStatement(sql11);
                        System.out.println("输入离职员工的工号：");
                        p11.setInt(1, sc.nextInt());
                        p11.executeUpdate();
                        System.out.println("删除成功，现有的员工信息：");
                        rs = sta.executeQuery("select * from Staff");
                        while (rs.next()) {
                            System.out.println("工号:" + rs.getString("Sno") + "\t姓名:" + rs.getString("Sname") + "\t电话号码:" + rs.getString("Sphone") + "\t年龄:" + rs.getInt("Sage") + "\t地址:" + rs.getString("Saddress"));
                        }
                        break;
                    case "修改员工信息":
                        String sql12 = "update Staff set Sname=?,Sphone=?,Sage=?,Saddress=? where Sno=?";
                        PreparedStatement p12 = con.prepareStatement(sql12);
                        System.out.println("输入修改后的员工姓名，电话号码，年龄和地址和所需修改的员工的工号：");
                        p12.setString(1, sc.next());
                        p12.setString(2, sc.next());
                        p12.setInt(3, sc.nextInt());
                        p12.setString(4, sc.next());
                        p12.setString(5, sc.next());
                        p12.executeUpdate();
                        System.out.println("修改成功，修改后的所有员工信息：");
                        rs = sta.executeQuery("select * from Staff");
                        while (rs.next()) {
                            System.out.println("工号:" + rs.getString("Sno") + "\t姓名:" + rs.getString("Sname") + "\t电话号码:" + rs.getString("Sphone") + "\t年龄:" + rs.getInt("Sage") + "\t地址:" + rs.getString("Saddress"));
                        }
                        break;
                    case "查询员工信息":
                        System.out.println("现在的所有员工信息为：");
                        rs = sta.executeQuery("select * from Staff");
                        while (rs.next()) {
                            System.out.println("工号:" + rs.getString("Sno") + "\t姓名:" + rs.getString("Sname") + "\t电话号码:" + rs.getString("Sphone") + "\t年龄:" + rs.getInt("Sage") + "\t地址" + rs.getString("Saddress"));
                        }
                        break;
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            } finally {
                try {
                    if (con != null) con.close();
                    if (sta != null) sta.close();
                    if (rs != null) rs.close();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }

        }
    }
}